<?php
/**
 * Created by JetBrains PhpStorm.
 * User: SonTung
 * Date: 10/06/2013
 * Time: 12:11
 * To change this template use File | Settings | File Templates.
 */

class AutoComplete extends CAction {
    public $model_class;
    public $field;
    public function run() {
        $res = array();
        $model = $this->model_class;
        if (isset($_GET['term'])){
            $term = $_GET['term'];
            $criteria = new CDbCriteria;
            $criteria->condition = "{$this->field} LIKE :term";
            $criteria->params = array(":term"=>"%$term%");
            $rs = $model::model()->findAll($criteria);
            foreach ($rs as $r)
                $res[] = $r->{$this->field};
        }
        echo CJSON::encode($res);
        Yii::app()->end();
    }
}